package br.com.imob.modelo;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Gilton Nascimento
 */
@Entity
@Table(name = "\"CONTRATO\"", catalog = "imob", schema = "public")
@NamedQueries({
    @NamedQuery(name = "Contrato.findAll", query = "SELECT c FROM Contrato c"),
    @NamedQuery(name = "Contrato.findByContratoId", query = "SELECT c FROM Contrato c WHERE c.contratoId = :contratoId"),
    @NamedQuery(name = "Contrato.findByDataContrato", query = "SELECT c FROM Contrato c WHERE c.dataContrato = :dataContrato"),
    @NamedQuery(name = "Contrato.findByDataTermino", query = "SELECT c FROM Contrato c WHERE c.dataTermino = :dataTermino"),
    
    @NamedQuery(name = "Contrato.findByCategoria", query = "SELECT c FROM Contrato c WHERE c.categoria = :categoria"),
    @NamedQuery(name = "Contrato.findLikeByCategoria", query = "SELECT c FROM Contrato c WHERE c.categoria Like :categoria"),
    
    @NamedQuery(name = "Contrato.findByQtdParcela", query = "SELECT c FROM Contrato c WHERE c.qtdParcela = :qtdParcela"),
    @NamedQuery(name = "Contrato.findByExpiracaoConta", query = "SELECT c FROM Contrato c WHERE c.expiracaoConta = :expiracaoConta"),
    @NamedQuery(name = "Contrato.findByLicensaVlr", query = "SELECT c FROM Contrato c WHERE c.licensaVlr = :licensaVlr"),
    @NamedQuery(name = "Contrato.findByQtdLicensa", query = "SELECT c FROM Contrato c WHERE c.qtdLicensa = :qtdLicensa"),
    @NamedQuery(name = "Contrato.findByValorTotal", query = "SELECT c FROM Contrato c WHERE c.valorTotal = :valorTotal")})
    
public class Contrato extends ControlEntities implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
//  <editor-fold defaultstate="collapsed" desc="Geradores de Sequencias">
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_STORE")
    @SequenceGenerator(name = "SEQ_STORE", sequenceName = "next_contrato")// </editor-fold>
    @Basic(optional = false)
    @Column(name = "contrato_id", nullable = false)
    private Integer contratoId;
    
    @Basic(optional = false)
    @Column(name = "data_contrato", nullable = false)
    @Temporal(TemporalType.DATE)
    private Date dataContrato;

    @Column(name = "data_termino")
    @Temporal(TemporalType.DATE)
    private Date dataTermino;
    
    @Column(name = "categoria", length = 20)
    private String categoria;
    
    @Column(name = "qtd_parcela")
    private Integer qtdParcela;
    
    @Column(name = "expiracao_conta")
    @Temporal(TemporalType.DATE)
    private Date expiracaoConta;
    
    @Basic(optional = false)
    @Column(name = "licensa_vlr", nullable = false)
    private double licensaVlr;
    
    @Basic(optional = false)
    @Column(name = "qtd_licensa", nullable = false)
    private int qtdLicensa;
    
    @Column(name = "valor_total", precision = 17, scale = 17)
    private Double valorTotal;
    
    @JoinColumn(name = "plano_id", referencedColumnName = "plano_id", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Plano plano;
    
    @JoinColumn(name = "conta_id", referencedColumnName = "conta_id", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Conta conta;

    public Contrato() { }

    public Contrato(Integer contratoId) { this.contratoId = contratoId; }

    public Contrato(String tpStr, String arg) {
    	if("dataContrato".equals(tpStr))dataContrato = str2Date(arg);
    	if("dataTermino".equals(tpStr))	dataTermino = str2Date(arg);
    	if("categoria".equals(tpStr))	categoria = arg;
    	if("expirado".equals(tpStr))	expiracaoConta = str2Date(arg);
    	if("licensas".equals(tpStr))	qtdLicensa = str2Integer(arg);
    	if("parcelas".equals(tpStr))	qtdParcela = str2Integer(arg);
    	if("valor".equals(tpStr))		licensaVlr = str2Double(arg);
    	if("valorTotal".equals(tpStr))	valorTotal = str2Double(arg);
    }
    
    public Contrato(int id, String tpStr, String arg) {
    	this(tpStr, arg);
    	contratoId = id;
    }
    
    public Contrato(
    			String dtContrato, String dtTermino, String categ, 
    			int licensas, int parcelas, double vlr, double vlrTotal,	
    			String expirado 
    			) {
        dataContrato = str2Date(dtContrato);
        dataTermino= str2Date(dtTermino);
        categoria = categ;
        qtdLicensa = licensas;
        qtdParcela = parcelas;
        licensaVlr = vlr;
        valorTotal = vlrTotal;
        expiracaoConta = str2Date(expirado);
    }
    
    public Contrato(
    		int id, String dtContrato, String dtTermino, String cat,
    		int licensas, int parcelas, double vlr, double vlrTotal,
    		String expirado 
    		) {
    	this(dtContrato, dtTermino, cat, licensas, parcelas, vlr, vlrTotal, expirado);
    	contratoId = id;
    }

	//    Getts and Setts
    public Integer getContratoId() {
        return contratoId;
    }

    public void setContratoId(Integer contratoId) {
        this.contratoId = contratoId;
    }

    public String getDtContrato() { return date2String(dataContrato); }
    public Date getDataContrato() {
        return dataContrato;
    }

    public void setDataContrato(Date dataContrato) {
        this.dataContrato = dataContrato;
    }
    
    public String getDtTermino() { return date2String(dataTermino); }
    public Date getDataTermino() {
        return dataTermino;
    }

    public void setDataTermino(Date dataTermino) {
        this.dataTermino = dataTermino;
    }

    public String getCategoria() {
        return categoria;
    }

    public void setCategoria(String categoria) {
        this.categoria = categoria;
    }

    public Integer getQtdParcela() {
        return qtdParcela;
    }

    public void setQtdParcela(Integer qtdParcela) {
        this.qtdParcela = qtdParcela;
    }

    
    public String getExpirado(){ return date2String(expiracaoConta); }
    public Date getExpiracaoConta() {
        return expiracaoConta;
    }

    public void setExpiracaoConta(Date expiracaoConta) {
        this.expiracaoConta = expiracaoConta;
    }

    public double getLicensaVlr() {
        return licensaVlr;
    }

    public void setLicensaVlr(double licensaVlr) {
        this.licensaVlr = licensaVlr;
    }

    public int getQtdLicensa() {
        return qtdLicensa;
    }

    public void setQtdLicensa(int qtdLicensa) {
        this.qtdLicensa = qtdLicensa;
    }

    public Double getValorTotal() {
        return valorTotal;
    }

    public void setValorTotal(Double valorTotal) {
        this.valorTotal = valorTotal;
    }

    public Plano getPlano() {
        return plano;
    }

    public void setPlano(Plano plano) {
        this.plano = plano;
    }

    public Conta getConta() {
        return conta;
    }

    public void setConta(Conta conta) {
        this.conta = conta;
    }
    
    public Object[] getValues() {
    	Object[] values = {
	    			contratoId, dataContrato, dataTermino, 
					categoria, qtdLicensa, qtdParcela, licensaVlr, 
					valorTotal, expiracaoConta 
				};
    	return values;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (contratoId != null ? contratoId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Contrato)) {
            return false;
        }
        Contrato other = (Contrato) object;
        if ((this.contratoId == null && other.contratoId != null) || (this.contratoId != null && !this.contratoId.equals(other.contratoId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "br.com.imob.modelo.Contrato[contratoId=" + contratoId + "]";
    }

}
